What is import-fresh?
The import-fresh npm package is used to import a module afresh, bypassing the cache that Node.js uses for require calls. This is particularly useful when you need to ensure that you're getting a fresh version of the module, for instance, when you're testing or when you want to reload configuration files that may have changed.
What are import-fresh's main functionalities?
Bypass require cache
This feature allows you to bypass the require cache and freshly import a module. It's useful when you want to reload a module to reflect any changes made to it since it was first imported.
const importFresh = require('import-fresh');
const freshConfig = importFresh('./config');
Other packages similar to import-fresh
proxyquire
Proxyquire is a package that allows you to override dependencies during testing. It provides a way to stub out modules similar to import-fresh, but with an emphasis on mocking for testing purposes.
decache
Decache is a package that lets you delete modules from the require cache. It is similar to import-fresh in that it allows for modules to be re-required as if they were never cached, but it does not provide a direct method to re-import a module.
clear-require
Clear-require is a package that clears a module from the require cache. It is similar to import-fresh, but instead of re-importing the module, it simply clears it from the cache, requiring an additional call to 'require' to reload the module.
import-fresh
Import a module while bypassing the cache
Useful for testing purposes when you need to freshly import a module.
Install
$ npm install --save import-fresh
Usage
let i = 0;
module.exports = () => ++i;
const importFresh = require('import-fresh');
require('./foo')();
require('./foo')();
importFresh('./foo')();
importFresh('./foo')();
Related
License
MIT © Sindre Sorhus